package com.guigu.www.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.pojo.File;
import com.guigu.www.pojo.Module;
import com.guigu.www.pojo.ModuleDetails;
import com.guigu.www.pojo.PageVo;
import com.guigu.www.service.FileService;
import com.guigu.www.service.ModuleService;
import com.guigu.www.service.imp.IdUtils;
import jdk.nashorn.internal.objects.annotations.Where;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
public class FileController {
    //获取流水号
    @Autowired
    IdUtils idUtils;
    @Autowired
    FileService fileService;
    @Autowired
    ModuleService moduleService;
    @RequestMapping("/queryFileAll.action")
    public List<File> queryFileAll() {
        return fileService.queryFileAll();
    }

    //查询下拉框的值
    @RequestMapping("/queryFileSel.action")
    public List<File> queryFileSel() {
        return fileService.queryFileSel();

    }

    @RequestMapping("/addFile.action")
    //添加
    public Map  addFile(File file){
        Date date=new Date();
        file.setRegistertime(date);
        String[] split = file.getFirstkindid().split(",");
        String[] split1 = file.getSecondkindid().split(",");
        String[] split2 = file.getThirdkindid().split(",");
        /*一级菜单的显示值和实际值*/
        file.setFirstkindid(split[0]);
        file.setFirstkindname(split[1]);
        /*二级菜单的显示值和实际值*/
        file.setSecondkindid(split1[0]);
        file.setSecondkindname(split1[1]);
        /*三级菜单的显示值和实际值*/
        file.setThirdkindid(split2[0]);
        file.setThirdkindname(split2[1]);
        //设置流水号
        file.setProductid(idUtils.getPRODUCT_ID(file));

        Map map=new HashMap();

        boolean result=fileService.addFile(file);

        if(result){
            map.put("msg","提交成功");
        }else{
            map.put("msg","提交失败");
        }
        return map;
    }

    @RequestMapping("/saveFile.action")
    //mybatis-plus方法添加
    public Map  saveFile(File file){
        Map map=new HashMap();

        boolean result=fileService.save(file);

        if(result){
            map.put("msg","提交成功");
        }else{
            map.put("msg","提交失败");
        }
        return map;
    }

    //id查询
    @RequestMapping("/queryByFileL.action")
    public File queryByFileL(String id){
        return fileService.queryByFileL(id);
    }

    //修改
    @RequestMapping("/updateFileL.action")
    public Map updateFileL(@RequestBody File file){
        Map map=new HashMap();

        boolean result=fileService.updateFileL(file);

        if(result){
            map.put("msg","提交成功");
        }else{
            map.put("msg","提交失败");
        }
        return map;
    }
    
    //安全库存查询通过的产品
    @RequestMapping("/queryByFileStock.action")
    public PageVo<File> queryByFileStock(@RequestParam(value = "pageno",defaultValue = "1") int pageno,
                                         @RequestParam(value = "pagesize",defaultValue = "5") int pagesize,
                                         File file){
        IPage<File> fileIPage = fileService.queryBystock(pageno, pagesize, file);
        //创建对象
        PageVo<File> pageVo=new PageVo<File>();
        pageVo.setRows(fileIPage.getRecords());
        pageVo.setTotal(fileIPage.getTotal());
        return pageVo;
    }

    //安全库存查询通过的产品
    @RequestMapping("/queryByFileStockather.action")
    public PageVo<File> queryByFileStockather(@RequestParam(value = "pageno",defaultValue = "1") int pageno,
                                         @RequestParam(value = "pagesize",defaultValue = "5") int pagesize,
                                         File file){
        IPage<File> fileIPage = fileService.queryBystockather(pageno, pagesize, file);
        //创建对象
        PageVo<File> pageVo=new PageVo<File>();
        pageVo.setRows(fileIPage.getRecords());
        pageVo.setTotal(fileIPage.getTotal());
        return pageVo;
    }
    //假删
    @RequestMapping("/delFileL.action")
    public Map delFileL(String id){
        Map map=new HashMap();

        boolean result=fileService.delFileL(id);

        if(result){
            map.put("msg","提交成功");
        }else{
            map.put("msg","提交失败");
        }
        return map;
    }


        //用删除状态C001-0来做查询
        @RequestMapping("/queryByDelAll.action")
        public PageVo<File> queryByDelAll(File file,
                                         @RequestParam(value = "pagenum",defaultValue = "1") int pagenum,
                                         @RequestParam(value = "pagesize",defaultValue = "5")  int pagesize){
        //条件查询
        QueryWrapper<File> queryWrapper=new QueryWrapper<File>();
        queryWrapper.eq("delete_tag","C001-0");

        //模糊查询
        if(StringUtils.isNotBlank(file.getProductname())){
            queryWrapper.like("product_name",file.getProductname());
        }

        Page<File> page = fileService.page(new Page<File>(pagenum, pagesize),queryWrapper);

        PageVo<File> pageVo =new PageVo<File>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());

        return pageVo;
    }

    //恢复
    @RequestMapping("/recover.action")
    public Map recover(String id){
        Map map=new HashMap();

        boolean result=fileService.recover(id);

        if(result){
            map.put("msg","恢复成功");
        }else{
            map.put("msg","提交失败");
        }
        return map;
    }

    //用删除状态C001-1来做查询
    @RequestMapping("/queryByDeleteTag.action")
    public PageVo<File> queryByDeleteTag(File file,
                                      @RequestParam(value = "pagenum",defaultValue = "1") int pagenum,
                                      @RequestParam(value = "pagesize",defaultValue = "5")  int pagesize){
        //条件查询
        QueryWrapper<File> queryWrapper=new QueryWrapper<File>();
        queryWrapper.eq("delete_tag","C001-1");

        //模糊查询
        if(StringUtils.isNotBlank(file.getProductname())){
            queryWrapper.like("product_name",file.getProductname());
        }

        Page<File> page = fileService.page(new Page<File>(pagenum, pagesize),queryWrapper);

        PageVo<File> pageVo =new PageVo<File>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());

        return pageVo;
    }

    //根据产品编号查询
    @GetMapping("queryByFileStockId.action/{id}")
    public File queryByFileStockId(@PathVariable int id){
        //获取数据
        return  fileService.getById(id);
    }
    //假删
    @RequestMapping("/delTrueL.action")
    public Map delTrueL(String id){
        Map map=new HashMap();

        boolean result=fileService.delTrueL(id);

        if(result){
            map.put("msg","删除成功");
        }else{
            map.put("msg","删除失败");
        }
        return map;
    }

    //用审核状态S001-0来查询
    @RequestMapping("/queryByCheckTag.action")
    public List<File> queryByCheckTag() {
        return fileService.queryByCheckTag();
     }

    //用审核状态S001-0来做修改
    @RequestMapping("/updateCheckTag.action")
    public Map updateCheckTag(@RequestBody File file){
        Map map=new HashMap();

        boolean result=fileService.updateCheckTag(file);

        if(result){
            map.put("msg","复核成功");
        }else{
            map.put("msg","修改失败");
        }
        return map;
    }

    @RequestMapping("/queryByType.action")
    //物料组成设计单  按用途类型商品来查
    public List<File> queryByType(){
        return fileService.queryByType();
    }

    //获取方法根据id查询添加到入库申请页面里
    @RequestMapping("/queryByIdaddather.action/{id}")
    public File queryByIdaddather(@PathVariable  int id){
        //获取方法
        File byId = fileService.getById(id);
        return  byId;
    }

    @RequestMapping("/queryByTypeWuLiao.action")
    //物料组成设计单  按用途类型物料来查
    public List<File> queryByTypeWuLiao(){
        return fileService.queryByTypeWuLiao();
    }

    //添加到产品物料组成明细表
    @RequestMapping("/addModuleDetails.action")
    public Map addModuleDetails(@RequestBody ArrayList<ModuleDetails> arr){
        Map map=new HashMap();
        ModuleDetails moduleDetails1=new ModuleDetails();

        for (ModuleDetails moduleDetails : arr) {
            System.out.println(moduleDetails);
            moduleDetails1.setParentid(moduleDetails.getParentid());
            moduleDetails1.setDetailsnumber(moduleDetails.getDetailsnumber());
            moduleDetails1.setProductid(moduleDetails.getProductid());
            moduleDetails1.setProductname(moduleDetails.getProductname());
            moduleDetails1.setAmount(moduleDetails.getAmount());
            moduleDetails1.setCostprice(moduleDetails.getCostprice());
            moduleDetails1.setSubtotal(moduleDetails.getSubtotal());
        }

        boolean result=fileService.addModuleDetails(moduleDetails1);

        if(result){
            map.put("msg","添加成功");
        }else{
            map.put("msg","添加失败");
        }
        return map;
    }

    //修改一下产品档案的物料组成标志W001-0变为W001-1
    @RequestMapping("/updateDesignModuleTag.action")
    public Map updateDesignModuleTag(String zdid){
        System.out.println(zdid);

        Map map=new HashMap();

        boolean result=fileService.updateDesignModuleTag(zdid);

        if(result){
            map.put("msg","制定成功");
        }else{
            map.put("msg","修改失败");
        }
        return map;
    }

    @RequestMapping("/queryModuleCheckTagLS.action")
    //产品物料组成查状态 查正常
    public List<Module> queryModuleCheckTagLS(){
        return fileService.queryModuleCheckTagLS();
    }

    //<!--添加产品物料组成表里-->
    @RequestMapping("/addModuleL.action")
    public Map addModuleL(@RequestBody ArrayList<Module> arr2){
        Map map=new HashMap();
        Module module1=new Module();

        for (Module module : arr2) {
            System.out.println(module);
            module1.setProductid(module.getProductid());
            module1.setProductname(module.getProductname());
            module1.setCostpricesum(module.getCostpricesum());
            module1.setRegistertime(module.getRegistertime());
        }

        boolean result=fileService.addModule(module1);

        if(result){
            map.put("msg","添加成功");
            map.put("id",module1.getId());
        }else{
            map.put("msg","添加失败");
        }
        return map;
    }

    //根据产品物料组成的id来查询他的物料
    @RequestMapping("/queryByModuleId.action/{id}")
    public List<ModuleDetails> queryByModuleId(@PathVariable String id){
        return fileService.queryByModuleId(id);
    }

    //修改产品物料组成表审核的状态
    @RequestMapping("updateByModuleCheckTag.action")
    public Map updateByModuleCheckTag(Module module){
        Map map=new HashMap();

        boolean result=fileService.updateByModuleCheckTag(module);

        if(result){
            map.put("msg","通过成功");
        }else{
            map.put("msg","通过失败");
        }
        return map;
    }

    //修改产品档案审核的状态
    @RequestMapping("updateByFileCheckTag.action")
    public Map updateByFileCheckTag(File file){
        Map map=new HashMap();

        boolean result=fileService.updateByFileCheckTag(file);

        if(result){
            map.put("msg","通过成功");
        }else{
            map.put("msg","通过失败");
        }
        return map;
    }


    //分页条件查询
    @RequestMapping("/queryByModuleT.action")
    public PageVo<Module> queryByModuleT(Module module,
                                            @RequestParam(value = "pagenum",defaultValue = "1") int pagenum,
                                            @RequestParam(value = "pagesize",defaultValue = "5")  int pagesize){

        //条件查询
        QueryWrapper<Module> queryWrapper=new QueryWrapper<Module>();
        if(StringUtils.isNotBlank(module.getProductname())){
            queryWrapper.like("product_name",module.getProductname());
        }

        if(StringUtils.isNotBlank(module.getChecktag())){
            queryWrapper.like("check_tag",module.getChecktag());
        }

        Page<Module> page = moduleService.page(new Page<Module>(pagenum, pagesize),queryWrapper);

        PageVo<Module> pageVo =new PageVo<Module>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());

        return pageVo;
    }

    //查询file表里type是物料的
    @RequestMapping("/filequery22.action")
    public List<File> filequery22() {
        return fileService.queryByFileWuliao();
    }

    //根据产品编号查询
    @RequestMapping("QueryFileByProductId.action/{productId}")
    public File QueryFileByProductId(@PathVariable String productId){
     return fileService.QueryFileByProductId(productId);
    }

    //查询物料变跟   需要审核通过的或正在变更的物料组成设计单
    @GetMapping("moduleupdate.action")
    public PageVo<Module> moduleupdate(Module module,
                                       @RequestParam(value = "pagenum",defaultValue ="1" ) int pagenum,
                                       @RequestParam(value = "pagesize",defaultValue = "5") int pagesize){
        PageVo<Module> page=moduleService.moduleupdate(module,pagenum,pagesize);
        return page;
    }

    //修改产品档案审核的状态 S001-1改为S001-0
    @RequestMapping("updateProducLSuccess.action")
    public Map updateProducLSuccess(int id){
        Map map=new HashMap();

        boolean result=fileService.updateProducLSuccess(id);

        if(result){
            map.put("msg","通过成功");
        }else{
            map.put("msg","通过失败");
        }
        return map;
    }
}
